<?php
namespace APP\C;

class Account extends Controller
{
    public function indexAction()
    {
        $this->router('/account/login');
    }


    /**
     * 显示登陆
     */
	public function loginAction()
	{
        $key = '@#54465SDGFWEY67hyu787';
        $errMsg = '';

        // 是否已登陆
        if (\SF\Session\Session::get(self::SESS_EMAIL)) {
            $this->router->redirect('/my');
        }

        // 检测自动登陆
        if (isset($_COOKIE[self::COOK_AUTO])) {
            $rem = explode(';', $_COOKIE[self::COOK_AUTO]);
            if (isset($rem[2]) && $rem[2] == md5($rem[0] . $rem[1] . $key)) {
                $this->_loginSuccess($rem[0], $rem[1]);
            }
        }

        // 登陆验证
        if ($this->request->isPost()) {
            $input = new \APP\PUT\Login($this->request->getPost());
            if ($input->isValid()) {
                $this->view->email = $input->email;
                $sql = 'SELECT username, password FROM user_info WHERE email="'. $input->email .'"';
                if (true == ($uinfo = $this->getDb()->fetchRow($sql))) {
                    if ($uinfo['password'] == md5($input->password)) {
                        // 记住登陆
                        if ($this->request->getPost('rem')) {
                            setcookie(
                                self::COOK_AUTO,
                                $input->email .';'. $uinfo['username'] .';'. md5($input->email . $uinfo['username'] . $key),
                                time() + 3600*24*14
                            );
                        }
                        $this->_loginSuccess($input->email, $uinfo['username']);
                    } else {
                        $errMsg = '密码错误';
                    }
                } else {
                    $errMsg = '邮箱不存在';
                }
            } else {
                $errMsg = $input->getMessage();
            }
        }

        $this->view->errMsg = $errMsg;
		$this->view->render('login.tpl');
	}

    protected function _loginSuccess($email, $username)
    {
        \SF\Session\Session::set(self::SESS_EMAIL, $email);
        \SF\Session\Session::set(self::SESS_NAME, $username);
        $this->router->redirect('/my');
    }


    /**
     * 显示注册
     */
    public function registerAction()
    {
        $this->view->render('register.tpl');
    }

    /**
     * 显示密码修改
     */
    public function pwdAction()
    {
    }

    
    /**
     * 退出登陆
     */
    public function logoutAction()
    {
        \SF\Session\Session::destroy();
        setcookie(self::COOK_AUTO, '');
        $this->router->redirect('/');
    }


    /**
     * 执行注册
     */
    public function doregAction()
    {
        echo '暂时还不能注册';
        exit;
    }

    /**
     * 执行密码修改
     */
    public function dopwdAction()
    {
    }

    /**
     * 忘记密码
     */
    public function forgetAction()
    {
    }
}